/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.${module};

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

<#if (tableMetaData.primaryKeys?size > 0)>
import com.unis.common.util.TemplateUtil;
</#if>
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.${module}.${classname};

/**
 * @author ${user} (email:${email})
 * @since ${createDate}
 */
<#assign instance = "${classname?lower_case}">
@Service("${instance}Service")
public class ${classname}ServiceImpl implements ${classname}Service {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of ${classname}
     ****************************************/
    public void insert(${classname} ${instance}) throws Exception {
        <#list tableMetaData.primaryKeys as pk>
        ${instance}.set${pk?cap_first}(TemplateUtil.genUUID());
        </#list>
        iDataSet4iBatis.insert(${instance});
    }

    /****************************************
     * Update a instace of ${classname}
     ****************************************/
    public void update(${classname} ${instance}) throws Exception {
        iDataSet4iBatis.update(${instance});
    }

    /****************************************
     * Delete a instace of ${classname}
     ****************************************/
    public void delete(${classname} ${instance}) throws Exception {
        iDataSet4iBatis.delete(${instance});
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(${classname} ${instance}) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(${classname} ${instance}) throws Exception {
        String statementId = ${instance}.getStatmentId();
        String mainId = ${instance}.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> ${instance}List = new ArrayList<BaseModel>();
        for (String id : ids) {
            ${classname} model = new ${classname}();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            ${instance}List.add(model);
        }
        iDataSet4iBatis.batchDelete(${instance}List);
    }

    /****************************************
     * Query instaces of ${classname}
     ****************************************/
    public List<BaseModel> queryAsList(${classname} ${instance}) throws Exception {
        String queryListStatement = "";
        if (${instance}.isPaging()) {
            queryListStatement = ${instance}.getStatmentId();
            ${instance}.setStatmentId("${classname}.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(${instance});
            ${instance}.setTotalCount(baseModel.getTotalCount());
            ${instance}.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(${instance});
    }

    /****************************************
     * Query a instaces of ${classname}
     ****************************************/
    public BaseModel queryAsObject(${classname} ${instance}) throws Exception {
        return iDataSet4iBatis.queryAsObject(${instance});
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
